﻿using AhpilyServer;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    public partial class ServerManager:Singleton<ServerManager>
    {
        private const string ip = "172.26.223.134";
        //private const string ip = "192.168.4.234";
        //private const string ip = "192.168.4.166";
        private const int port = 8000;
        private const int MaxPeer = 100;

        ServerPeer server;

        public ServerManager()
        {
            server = new ServerPeer();
            server.SetApplication(new HandlerCenter());//指定所关联的应用
        }

        //启动服务器
        public void StartUp()
        {
            server.Start(ip, port, MaxPeer);
        }

        #region 静态工具方法
        static void Send<T>(ClientPeer client, Protocol cmd, T t) where T : ProtoBuf.IExtensible
        {
            if(client!=null)
            {
                client.Send((int)cmd, Util.Serilize<T>(t));
            }
        }

        public static void SendClient<T>(ClientPeer client, Protocol cmd, T t) where T : ProtoBuf.IExtensible
        {
            Send(client,cmd,t);
        }
        //发送给所有人
        public static void SendAllClients<T>(ClientPeer client, Protocol cmd, T t) where T : ProtoBuf.IExtensible
        {
            List<ClientPeer> clients = AccountCache.Instance.GetAllActors();
            for (int i = 0; i < clients.Count; i++)
            {
                Send(clients[i], cmd, t);

            }
        }
        //发送给其他玩家
        public static void SendOther<T>(ClientPeer client, Protocol cmd, T t) where T : ProtoBuf.IExtensible
        {
            List<ClientPeer> clients = AccountCache.Instance.GetAllActors();
            for (int i = 0; i < clients.Count; i++)
            {
                if (client != clients[i])
                {
                    Send(clients[i], cmd, t);
                }
            }
        }
        #endregion
    }
}
